package com.cdut.hezhisu.dribbble;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.speech.tts.TextToSpeech;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cdut.hezhisu.dribbble.bean.Works;
import com.cdut.hezhisu.dribbble.util.PictureUtil;
import com.cdut.hezhisu.dribbble.util.ScreenUtil;
import com.cdut.hezhisu.dribbble.view.CustomProgressBar;
import com.cdut.hezhisu.dribbble.view.MultiColorView;
import com.cdut.hezhisu.dribbble.view.RoundCornerImageView;
import com.cdut.hezhisu.dribbble.view.ScaleImageView;
import com.facebook.cache.common.CacheKey;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.imagepipeline.request.Postprocessor;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yiqijiao on 2015/12/23.
 */
public class PictureListAdapter extends BaseAdapter{

    private Context mContext;
    private int mItemWidth;
    private int mItemHeight;
    private List<Works> mWorkses;
    private PictureViewHolder mHolder;
    private OnClickMenuListener mListener;
    private ImagePipeline mImagePipeline = Fresco.getImagePipeline();
    public PictureListAdapter(Context context){
        this.mContext = context;
        this.mItemWidth = ScreenUtil.getScreenWidth(context) - ScreenUtil.dip2px(context,32);
        this.mItemHeight = this.mItemWidth * 3 / 4;
        this.mWorkses = new ArrayList<Works>();
    }

    /**
     * 刷新数据
     * @param workses
     */
    public void refreshData(List<Works> workses){
        this.mWorkses = workses;
        this.notifyDataSetChanged();
    }

    public void addDataList(List<Works> workses){
        this.mWorkses.addAll(workses);
        this.notifyDataSetChanged();
    }

    public void setOnClickMenuListener(OnClickMenuListener listener){
        this.mListener = listener;
    }

    @Override
    public int getCount() {
        return this.mWorkses.size();
    }

    @Override
    public Works getItem(int position) {
        return this.mWorkses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView =  LayoutInflater.from(this.mContext).inflate(R.layout.picture_list_item,null);
            mHolder = new PictureViewHolder();
            mHolder.mTvName = (TextView) convertView.findViewById(R.id.tv_name);
            mHolder.mIvPicture = (SimpleDraweeView) convertView.findViewById(R.id.iv_picture);
            mHolder.mTvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            mHolder.mIvAvatar = (SimpleDraweeView) convertView.findViewById(R.id.iv_avatar);
            mHolder.mTvDescribe = (TextView) convertView.findViewById(R.id.tv_describe);
            mHolder.mMultiColorView = (MultiColorView) convertView.findViewById(R.id.multi_color_view);
            mHolder.mIvMenu = (ImageView) convertView.findViewById(R.id.iv_menu);
            convertView.setTag(mHolder);
        }else{
            mHolder = (PictureViewHolder) convertView.getTag();
        }
        final Works works = this.mWorkses.get(position);
        mHolder.mIvAvatar.setImageURI(Uri.parse(works.avatar));
        mHolder.mTvName.setText(works.author);
        mHolder.mTvTitle.setText(works.title);
        if(TextUtils.isEmpty(works.describe)){
            mHolder.mTvDescribe.setVisibility(View.GONE);
        }else{
            mHolder.mTvDescribe.setVisibility(View.VISIBLE);
            mHolder.mTvDescribe.setText(works.describe);
        }
        mHolder.mIvPicture.setAspectRatio(1.3333f);
        final ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(works.picUrl))
                .setLocalThumbnailPreviewsEnabled(true)

                .build();
        final PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setAutoPlayAnimations(true)
                .build();
        mHolder.mIvPicture.setController(controller);
        GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(mContext.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(300)
                .setProgressBarImage(new CustomProgressBar())
                .build();
        mHolder.mIvPicture.setHierarchy(hierarchy);
        mHolder.mMultiColorView.removeAllViews();
        mHolder.mMultiColorView.addContacts(works.colors);
        mHolder.mIvMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(works.picUrl);
                }
            }
        });
        return convertView;
    }


    class PictureViewHolder
    {

        TextView mTvName;
        TextView mTvTitle;
        TextView mTvDescribe;
        SimpleDraweeView mIvAvatar;
        SimpleDraweeView mIvPicture;
        MultiColorView mMultiColorView;
        ImageView mIvMenu;
    }

    public interface OnClickMenuListener{

        void onClick(String url);


    }
}
